// pages/lookVennue/index/index.js
//获取应用实例
const app = getApp()
const api = require('../../../api/index.js')
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: [
      {
        'name': '分类',
        'item': 0
      },
      {
        'name': '区域',
        'item': 1
      },
      {
        'name': '场馆星级',
        'item': 2
      },
      {
        'name': '价格区间',
        'item': 3
      },
    ],
    searchReqData: {
      d_id: "",
      f_id: "",
      branch_star: "",
      price: "",
      page: 1
    },
    venueData: {
      branch: []
    },
    venueList: [],
    address: "", // 首页传过来的地址字段
    noData: false, // 如果服务器返回有数据  则为true
    classPosition: false, // 设置页面滚动选择框位置
    pagenum: 1,
    noMore: false,
    imgSrc: ""
  },
  page: function () {  //  用来每次分类选择重置 page
    this.data.searchReqData.page = 1;
  },
  bindPickerChange: function (e) { // 分类
    App.zhuge.track('找场馆-分类名称', {
      '分类': this.data.nav[0].name
    });
    this.data.nav[0].name = this.data.venueData.classify[e.detail.value].name;
    this.data.searchReqData.f_id = this.data.venueData.classify[e.detail.value].id;
    this.data.noMore = false;
    app.globalData.serchData = "";
    this.page();
    this.setData({
      nav: this.data.nav,
      searchReqData: this.data.searchReqData,
      venueList: [],
      pagenum: 1
    })
    console.log(this.data.searchReqData)
    this.getSearchVenueData(this.data.searchReqData)
  },
  bindPickerChangeA: function (e) {  // 区域
    App.zhuge.track('找场馆-区域名称', {
      '区域': this.data.nav[1].name
    });
    this.data.nav[1].name = this.data.venueData.region[e.detail.value].region_name;
    this.data.searchReqData.d_id = this.data.venueData.region[e.detail.value].region_id;
    this.data.noMore = false;
    app.globalData.serchData = "";
    this.page();
    this.setData({
      nav: this.data.nav,
      searchReqData: this.data.searchReqData,
      venueList: [],
      pagenum: 1
    })
    console.log(this.data.searchReqData)
    this.getSearchVenueData(this.data.searchReqData)
  },
  bindPickerChangeG: function (e) { //场馆星级
    App.zhuge.track('找场馆-场馆星级', {
      '场馆星级': this.data.nav[2].name
    });
    this.data.nav[2].name = this.data.venueData.branch_star[e.detail.value].name;
    this.data.searchReqData.branch_star = this.data.venueData.branch_star[e.detail.value].type;
    this.data.noMore = false;
    app.globalData.serchData = "";
    this.page();
    this.setData({
      nav: this.data.nav,
      searchReqData: this.data.searchReqData,
      venueList: [],
      pagenum: 1
    })
    console.log(this.data.searchReqData)
    this.getSearchVenueData(this.data.searchReqData)
  },
  bindPickerChangeM: function (e) { //价格区间
    App.zhuge.track('找场馆-价格区间', {
      '价格区间': this.data.nav[3].name
    });
    this.data.nav[3].name = this.data.venueData.price[e.detail.value].p_name;
    this.data.searchReqData.price = this.data.venueData.price[e.detail.value].p_type;
    this.data.noMore = false;
    app.globalData.serchData = "";
    this.page();
    this.setData({
      nav: this.data.nav,
      searchReqData: this.data.searchReqData,
      venueList: [],
      pagenum: 1
    })
    console.log(this.data.searchReqData)
    this.getSearchVenueData(this.data.searchReqData)
  },
  onUnload: function () {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)

    this.setData({
      imgSrc: api.imgPathUrl
    })
    let PianBackQuery = app.globalData.vennueQuery; // 评测页面参数
    let indexSearchQuery = app.globalData.IndexQuery; // 首页类别跳转获取参数
    let searchData = app.globalData.serchData; // 搜索框传来的值

    this.setData({
      nav: [
        {
          'name': '分类',
          'item': 0
        },
        {
          'name': '区域',
          'item': 1
        },
        {
          'name': '场馆星级',
          'item': 2
        },
        {
          'name': '价格区间',
          'item': 3
        }
      ],
      venueList: []
    })
    if (options == 3) {
      console.log(this.data.searchReqData)
      this.data.searchReqData.f_id = '';
      this.data.searchReqData.page = '';
      api.venueSearch(this.data.searchReqData, (res) => {
        console.log(res.data.result)
        this.setData({
          venueData: res.data.result,
          venueList: res.data.result.branch
        })
      })
    } else {
      if (searchData || PianBackQuery.id) {
        api.venueSearch(this.data.searchReqData, (res) => {
          delete res.data.result.branch
          console.log(res.data.result)
          this.setData({
            venueData: res.data.result,
          })
        })
        console.log("我带参数来了")
      } else {
        api.venueSearch(this.data.searchReqData, (res) => {
          console.log(res.data.result)
          this.setData({
            venueData: res.data.result,
            venueList: res.data.result.branch
          })
        })
        console.log("我没带参数来")
      }
    }
    // if (searchData || PianBackQuery.id) {
    //   api.venueSearch(this.data.searchReqData, (res) => {
    //     delete res.data.result.branch
    //     console.log(res.data.result)
    //     this.setData({
    //       venueData: res.data.result,
    //     })
    //   })
    //   console.log("我带参数来了")
    // } else {
    //   api.venueSearch(this.data.searchReqData, (res) => {
    //     console.log(res.data.result)
    //     this.setData({
    //       venueData: res.data.result,
    //       venueList: res.data.result.branch
    //     })
    //   })
    //   console.log("我没带参数来")
    // }

    //wx.showNavigationBarLoading(); 

    // 正常切换
    this.data.nav[0].name = "分类";
    if (!app.globalData.vennueQuery) { // 如果是从首页分类跳来的   就不执行默认数据
      this.getSearchVenueData(this.data.searchReqData)
    }
    this.setData({
      "nav": this.data.nav
    })
  },
  onPageScroll: function (e) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  eliminate:function(){
    this.data.noMore = false;
    this.data.searchReqData = {
      d_id: "",
      f_id: "",
      branch_star: "",
      price: "",
      page: 0
    }
    app.globalData.serchData = '';
    this.data.venueList = [];
    this.getSearchVenueData(this.data.searchReqData)
    this.setData({
      nav: [
        {
          'name': '分类',
          'item': 0
        },
        {
          'name': '区域',
          'item': 1
        },
        {
          'name': '场馆星级',
          'item': 2
        },
        {
          'name': '价格区间',
          'item': 3
        },
      ]
    })
    app.globalData.moreVenue = "";
  },

  onShow: function () {

    let Refresh = app.globalData.Refresh;
    if (Refresh == true){
         this.onLoad(3);
         app.globalData.Refresh = false;
         
    }
    let PianBackQuery = app.globalData.vennueQuery; // 评测页面参数
    let indexSearchQuery = app.globalData.IndexQuery; // 首页类别跳转获取参数
    let searchData = app.globalData.serchData; // 搜索框传来的值
    this.setData({
      address: app.globalData.currentCity.region_name
    })

    if (app.globalData.moreVenue){
      console.log("我是首页查看更多过来的 ，我要重置数据了")
      this.data.noMore = false;
      app.globalData.serchData = "";
      this.data.searchReqData = {
        d_id: "",
        f_id: "",
        branch_star: "",
        price: "",
        page: 0
      }
      this.data.venueList = [];
      this.getSearchVenueData(this.data.searchReqData)
      this.setData({
        nav: [
          {
            'name': '分类',
            'item': 0
          },
          {
            'name': '区域',
            'item': 1
          },
          {
            'name': '场馆星级',
            'item': 2
          },
          {
            'name': '价格区间',
            'item': 3
          },
        ]
      })
      app.globalData.moreVenue = "";
    }
    if (searchData) {
      this.data.noMore = false;
      console.log('我带着搜索框的值来了');
      this.search(searchData);
    
    }

    if (PianBackQuery.id) {
      this.data.noMore = false;
      console.log('我带品测页的参数来了，')
      app.globalData.serchData = ''
      this.data.nav[0].name = PianBackQuery.name;
      this.data.nav[1].name = "区域";
      this.data.nav[2].name = "场馆星级";
      this.data.nav[3].name = "价格区间";
      this.setData({
        "nav": this.data.nav,
        [`searchReqData.f_id`]: PianBackQuery.id,
        venueList: []
      })
      this.getSearchVenueData({ // 如果是从评测跳转来的  就请求搜索数据
        d_id: "",
        f_id: PianBackQuery.id,
        branch_star: "",
        price: "",
        page: 1
      }).then((res) => {
        this.data.venueList = [];
        this.setData({
          venueList: res.data.result.branch
        })

      })
      app.globalData.vennueQuery = {};
      return;
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    App.zhuge.track('找场馆页面卸载', {
      '页面卸载': '页面卸载'
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let searchData = app.globalData.serchData; // 搜索框传来的值
    if (searchData) return; // 如果搜索过来的  就不允许下拉
    if (this.data.noMore) return; // 如果没有数据  就不能再请求加载数据了
    this.data.searchReqData.page = ++this.data.pagenum;
    this.setData({
      searchReqData: this.data.searchReqData
    })
    this.getSearchVenueData(this.data.searchReqData)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getSearchVenueData: function (data) { // 按照条件搜索

    return new Promise((resolve, reject) => {
      // ... some code
      wx.showLoading({
        title: '加载中',
      })
      api.venueSearch(data, (res) => {

        if (res.data.result.branch.length <= 0) {
          this.data.noMore = true;
          wx.showToast({
            title: '没有数据了',
            icon: "none"
          })
        }
        this.setData({
          venueList: this.data.venueList.concat(res.data.result.branch)
        })
        this.judgeData();
        resolve(res);
        wx.hideLoading();
      })
    });

  },
  search: function (name) {
    let data = {
      name: name
    }

    api.indexSearch(data, (res) => {
      this.setData({
        venueList: res.data.result.branch
      })
      this.judgeData();

    })

  },
  judgeData: function () {
    let data = this.data.venueList.length || 0;
    if (data > 0) {
      console.log('我有数据，显示数据')
      this.setData({
        noData: false
      })
    } else {
      console.log('我没有数据，但是也不能给你一张大白脸')
      this.setData({
        noData: true
      })
    }
  },
  lookListData: function () {
    App.zhuge.track('找场馆列表点击', {
      '找场馆列表点击': '找场馆列表点击'
    });
  }
})